﻿//--------------------- Copyright Block ----------------------
/*

Hijri.cs 2011

C# Code By: Tareq Ateik

License: GNU General Public License, ver 3

TERMS OF USE:
	Permission is granted to use this code, with or
	without modification, in any website or application
	provided that credit is given to the original work
	with a link back to ateik.me

This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY.

PLEASE DO NOT REMOVE THIS COPYRIGHT BLOCK.

*/

using System;
public static class Hijri
{

    static double intPart(double floatNum)
    {
        if (floatNum < -0.0000001)
        {
            return Math.Ceiling(floatNum - 0.0000001);
        }
        return Math.Floor(floatNum + 0.0000001);
    }
    static string weekDay(double wdn)
    {
        if (wdn == 0)
        {
            return "الأثنين";
        }
        if (wdn == 1)
        {
            return "الثلاثاء";
        }
        if (wdn == 2)
        {
            return "الأربعاء";
        }
        if (wdn == 3)
        {
            return "الخميس";
        }
        if (wdn == 4)
        {
            return "الجمعة";
        }
        if (wdn == 5)
        {
            return "السبت";
        }
        if (wdn == 6)
        {
            return "الأحد";
        }
        return "";

    }
    public static DateTime GreToHij(DateTime arg)
    {
        double d = arg.Day;
        double m = arg.Month;
        double y = arg.Year;
        double l;
        double JD, jd;
        string wd;
        double n, j;
        if ((y > 1582) || ((y == 1582) && (m > 10)) || ((y == 1582) && (m == 10) && (d > 14)))
        {
            jd = intPart((1461 * (y + 4800 + intPart((m - 14) / 12))) / 4) + intPart((367 * (m - 2 - 12 * (intPart((m - 14) / 12)))) / 12) - intPart((3 * (intPart((y + 4900 + intPart((m - 14) / 12)) / 100))) / 4) + d - 32075;
        }
        else
        {
            jd = 367 * y - intPart((7 * (y + 5001 + intPart((m - 9) / 7))) / 4) + intPart((275 * m) / 9) + d + 1729777;
        }
        JD = jd;
        wd = weekDay(jd % 7);
        l = jd - 1948440 + 10632;
        n = intPart((l - 1) / 10631);
        l = l - 10631 * n + 354;
        j = (intPart((10985 - l) / 5316)) * (intPart((50 * l) / 17719)) + (intPart(l / 5670)) * (intPart((43 * l) / 15238));
        l = l - (intPart((30 - j) / 15)) * (intPart((17719 * j) / 50)) - (intPart(j / 16)) * (intPart((15238 * j) / 43)) + 29;
        m = intPart((24 * l) / 709);
        d = l - intPart((709 * m) / 24);
        y = 30 * n + j - 30;

        int Y = (int)y;
        int M = (int)m;
        int D = (int)d;
        return new DateTime(Y, M, D);

    }
    public static DateTime HijToGre(DateTime arg)
    {
        double d = arg.Day;
        double m = arg.Month;
        double y = arg.Year;
        double l;
        double JD, jd;
        string wd;
        double n, j, i, k;
        d = arg.Day;
        m = arg.Month;
        y = arg.Year;
        jd = intPart((11 * y + 3) / 30) + 354 * y + 30 * m - intPart((m - 1) / 2) + d + 1948440 - 385;
        JD = jd;
        wd = weekDay(jd % 7);
        if (jd > 2299160)
        {
            l = jd + 68569;
            n = intPart((4 * l) / 146097);
            l = l - intPart((146097 * n + 3) / 4);
            i = intPart((4000 * (l + 1)) / 1461001);
            l = l - intPart((1461 * i) / 4) + 31;
            j = intPart((80 * l) / 2447);
            d = l - intPart((2447 * j) / 80);
            l = intPart(j / 11);
            m = j + 2 - 12 * l;
            y = 100 * (n - 49) + i + l;
        }
        else
        {
            j = jd + 1402;
            k = intPart((j - 1) / 1461);
            l = j - 1461 * k;
            n = intPart((l - 1) / 365) - intPart(l / 1461);
            i = l - 365 * n + 30;
            j = intPart((80 * i) / 2447);
            d = i - intPart((2447 * j) / 80);
            i = intPart(j / 11);
            m = j + 2 - 12 * i;
            y = 4 * k + n + i - 4716;
        }

        int Y = (int)y;
        int M = (int)m;
        int D = (int)d;
        return new DateTime(Y, M, D);

    }
}